<?php
namespace app\index\service;

use app\index\model\BidModel;
use app\index\model\DeptModel;
use app\index\model\QianBanModel;
use app\index\model\QianPiaoModel;
use app\index\model\QianShouListModel;
use app\index\model\QianShouModel;
use app\index\model\QianZhiListModel;
use app\index\model\QianZhiModel;
use app\index\model\TouFanModel;
use app\index\model\TouJiaoModel;
use think\Db;

class QianService extends CommonService
{
    public function get_tou_jiao_ziqu(){
        return [
            1 => '工程负责人预付前期费',
            2 => '公司垫资',
            3 => '公司自主投标',
        ];
    }


    public function get_tou_jiao_ziqu_name($ziqu){
        $arr = $this->get_tou_jiao_ziqu();
        $str = isset($arr[$ziqu]) ? $arr[$ziqu] : '';
        return $str;
    }



    public function get_bid_qian_count($bidid=0, $pai_qian_zhi_id=0){
        $bidinfo = BidModel::where(['id'=>$bidid])->find();

        $idArr = [$bidid];
        if($bidinfo['jinglei'] == '分公司(办事处)'){
            $idArr = BidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'jinglei'=>'分公司(办事处)','gofuer'=>$bidinfo['gofuer']])->column('id');
        }

        if($bidinfo['jinglei'] == '分公司(办事处)'){
            $data['qian_shou_fee'] = QianBanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'gofuer'=>$bidinfo['gofuer']])->sum('money');
        }else{
            $data['qian_shou_fee'] = QianShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('money');
        }



        $data['qian_zhi_fee'] = 0;
        $data['qian_zhi_fee'] = QianZhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('money');
        $data['qian_zhi_fee'] += TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('koudaifee');

        $data['qian_piao_fee'] = QianPiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('money');

        $data['shou_bao_fee'] = QianShouListModel::alias('list')
            ->join('qian_shou qian','qian.id=list.shouid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['list.shouqianlei'=>1])
            ->where('qian.bidid','in',$idArr)
            ->sum('list.money');

        $data['tou_jiao_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('money');

        $data['ying_dian_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('dianfee');

        //	T08	已收垫资利息（元）	前期费收取	明细	金额	过滤工程名称/审核通过/款项类型=利息	合计
        $data['shou_dianli_fee'] = QianShouListModel::alias('list')
            ->join('qian_shou qian','qian.id=list.shouid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['list.shouqianlei'=>4])
            ->where('qian.bidid','in',$idArr)
            ->sum('list.money');

        $data['daishou_dianli_fee'] = round($data['ying_dian_fee'] - $data['shou_dianli_fee'], 2);
        $data['daishou_dianli_fee'] = $data['daishou_dianli_fee'] > 0 ? $data['daishou_dianli_fee'] : 0;

        $data['tou_fan_fee'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('money');

        $data['tui_bao_fee'] = QianZhiListModel::alias('list')
            ->join('qian_zhi qian','qian.id=list.zhiid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['list.zhiqianlei'=>5])
            ->where('qian.bidid','in',$idArr)
            ->sum('list.money');

        $data['daifu_kou_fee'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('koudaifee');

        $data['tou_fan_fee'] += $data['daifu_kou_fee'];

        $data['daifu_zhi_fee'] = QianZhiListModel::alias('list')
            ->join('qian_zhi qian','qian.id=list.zhiid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['list.zhiqianlei'=>2])
            ->where('qian.bidid','in',$idArr)
            ->sum('list.money');

        $data['daifu_heji_fee'] = round($data['daifu_kou_fee'] + $data['daifu_zhi_fee'], 2);

        $data['qian_run_fee'] = round($data['qian_shou_fee'] - $data['qian_zhi_fee'] - $data['tou_jiao_fee'] + $data['tou_fan_fee'], 2);

        $data['zhi_shen_fee'] = QianZhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>0])->where('bidid','in',$idArr)->where('id','neq', $pai_qian_zhi_id)->sum('money');

        $data['ke_zhiyu_fee'] = round($data['qian_run_fee'] - $data['zhi_shen_fee'], 2);


        return $data;
    }

    public function get_gofuer_ban_count($gofuer){
        $bidIdArr = BidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'jinglei'=>'分公司(办事处)','gofuer'=>$gofuer])->column('id');

        $data['ban_shou_fee'] = QianBanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'gofuer'=>$gofuer])->sum('money');


        $data['ban_zhi_fee'] = 0;
        $data['ban_zhi_fee'] = QianZhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$bidIdArr)->sum('money');
        $data['ban_zhi_fee'] += TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$bidIdArr)->sum('koudaifee');



        $data['tou_jiao_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'ziqu'=>1])->where('bidid','in',$bidIdArr)->sum('money');


        $jiaoIdArr = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'ziqu'=>1])->where('bidid','in',$bidIdArr)->column('id');
        $data['tou_fan_fee'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1])->where('jiaoid','in',$jiaoIdArr)->sum('money');

        $data['ban_yu_fee'] = round($data['ban_shou_fee'] - $data['ban_zhi_fee'] - $data['tou_jiao_fee'] + $data['tou_fan_fee'], 2);
        return $data;
    }




}
